import immutable from '../../../lib/immutable';
import _uiInterface from './uiInterface';
export const ELEMENT_TYPE_SEARCH = 'search';
export const EVENT_TYPE_SEARCH = 'EVENT_TYPE_SEARCH';// 搜索


const Search = {
    create: (metaData,
             uiInterface = _uiInterface,
             onRefreshQuery) => {
        if (!metaData || metaData.getIn(['element', 'type']) !== ELEMENT_TYPE_SEARCH) return undefined;
        const core = {
            metaData,
            id: metaData.getIn(['element', 'id']),
            querys: immutable.fromJS({}),
            uiInterface,
        };

        core.onFormClick = function onFormClick() {
            const element = metaData.get('element');
            const item = element.get('content_list');
            if (item && item.get('pageId') && item.get('listId')) {
                const params = { keywords: core.querys.get('keywords') };
                onRefreshQuery && onRefreshQuery({
                    pageId: item.get('pageId'),
                    urlMode: item.get('urlMode'),
                    params: { [item.get('listId')]: params, [element.get('id')]: params },
                });
            }
        };
        core.setQuery = (nQuerys) => {
            core.prevQuerys = core.querys;
            core.querys = nQuerys;
        };
        core.callUpdate = () => {
            if (!immutable.is(core.querys, core.prevQuerys)) {
                core.uiInterface.update(core);
            }
        };
        return core;
    },
};

export default Search;
